<template>
  <div class="item-manage">
    <!-- 上面的查询框 -->
    <div class="query-form">
      <el-form :inline="true" :model="queryForm" ref="form">
        <el-form-item label="项目名称" prop="itemName">
          <el-input v-model="queryForm.itemName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="项目分类" prop="status">
          <el-select v-model="queryForm.status" placeholder="请选择项目分类">
            <el-option :value="1" label="运营类项目"></el-option>
            <el-option :value="2" label="开发类项目"></el-option>
            <el-option :value="3" label="其它"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button @click="handleReset('form')">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="action">
        <el-button type="primary" @click="handleCreateItem">新增项目</el-button>
      </div>
    </div>
    <!-- 下面的表格 -->
    <div class="base-table">
      <!-- 表单 -->
      <el-table :data="itemList" :stripe="true" :border="true">
        <el-table-column label="项目名称" prop="itemName" />
        <el-table-column label="项目优先级" prop="priority">
          <template #default="scope">
            <el-rate v-model="scope.row.priority" disabled />
          </template>
        </el-table-column>
        <el-table-column
          label="项目分类"
          prop="classify"
          :formatter="formatter"
        />
        <el-table-column
          label="项目负责人"
          prop="leader"
          :formatter="formatterCommon"
          width="110"
        />
        <el-table-column
          label="项目执行人"
          prop="operator"
          :formatter="formatterCommon"
          width="110"
        />
        <el-table-column
          label="项目来源人"
          prop="source"
          :formatter="formatterCommon"
          width="110"
        />
        <el-table-column
          label="甲方对接人"
          prop="owner"
          :formatter="formatterCommon"
          width="110"
        />
        <el-table-column
          label="项目状态"
          prop="status"
          :formatter="formatterStatus"
        />
        <!-- 操作 -->
        <el-table-column label="操作" width="170">
          <template #default="scope">
            <el-button size="mini" type="primary" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="mini"
              @click="handleDel(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 新增项目 -->
    <el-dialog
      title="新增项目"
      v-model="showModal"
      @close="handleClose"
      :close-on-click-modal="false"
    >
      <el-form
        ref="dialogForm"
        :model="itemForm"
        label-width="130px"
        :rules="rules"
      >
        <!-- 项目名称 -->
        <el-form-item label="项目名称" prop="itemName">
          <el-input v-model="itemForm.itemName" placeholder="请输入项目名称" />
        </el-form-item>
        <!-- 项目优先级 -->
        <el-form-item label="项目优先级" prop="priority">
          <el-rate v-model="itemForm.priority" />
        </el-form-item>
        <!-- 项目分类 -->
        <el-form-item label="项目分类" prop="classify">
          <el-select v-model="itemForm.classify">
            <el-option :value="1" label="运营类项目"></el-option>
            <el-option :value="2" label="开发类项目"></el-option>
            <el-option :value="3" label="其它"></el-option>
          </el-select>
        </el-form-item>
        <!-- 项目负责人/电话 -->
        <el-form-item label="项目负责人/电话" prop="leader">
          <el-input
            v-model="itemForm.leader"
            placeholder="请输入项目负责人/电话(例：张三/13888888888)"
          ></el-input>
        </el-form-item>
        <!-- 项目执行人/电话 -->
        <el-form-item label="项目执行人/电话" prop="operator">
          <el-input
            v-model="itemForm.operator"
            placeholder="请输入项目执行人/电话(例：李四/13888888888)"
          ></el-input>
        </el-form-item>
        <!-- 项目来源人/电话 -->
        <el-form-item label="项目来源人/电话" prop="source">
          <el-input
            v-model="itemForm.source"
            placeholder="请输入项目来源人/电话(例：王五/13888888888)"
          ></el-input>
        </el-form-item>
        <!-- 甲方对接人/电话 -->
        <el-form-item label="甲方对接人/电话" prop="owner">
          <el-input
            v-model="itemForm.owner"
            placeholder="请输入甲方对接人/电话(例：赵六/13888888888)"
          ></el-input>
        </el-form-item>
        <!-- 项目状态 -->
        <el-form-item label="项目状态" prop="status">
          <el-select v-model="itemForm.status">
            <el-option :value="1" label="已签合同正在实施"></el-option>
            <el-option :value="2" label="已签合同未实施"></el-option>
            <el-option :value="3" label="未签合同正在实施"></el-option>
            <el-option :value="4" label="未签合同未实施"></el-option>
          </el-select>
        </el-form-item>
        <!-- 签订周期 -->
        <el-form-item label="项目签订周期" required>
          <el-col :span="11">
            <el-form-item prop="startTime">
              <el-date-picker
                type="date"
                placeholder="请选择项目签订起始日期"
                v-model="itemForm.startTime"
              />
            </el-form-item>
          </el-col>
          <el-col class="line" :span="2"> 至 </el-col>
          <el-col :span="11">
            <el-form-item prop="endTime">
              <el-date-picker
                placeholder="请选择项目签订结束日期"
                type="date"
                v-model="itemForm.endTime"
              />
            </el-form-item>
          </el-col>
        </el-form-item>
        <!-- 项目介绍 -->
        <el-form-item label="项目介绍" prop="desc">
          <el-input
            type="textarea"
            v-model="itemForm.desc"
            placeholder="请输入简短的项目描述(可不填)"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Item',
  data() {
    return {
      queryForm: {},
      itemForm: {
        status: 1,
        classify: 1
      },
      rules: {
        itemName: [
          { required: true, message: '请输入项目名称', trigger: 'blur' }
        ],
        priority: [
          {
            required: true, message: '请选择项目优先级', trigger: 'change'
          }
        ],
        leader: [
          {
            required: true, message: '请填写项目负责人名称及电话', trigger: 'blur'
          }
        ],
        operator: [
          {
            required: true, message: '请填写项目执行人名称及电话', trigger: 'blur'
          }
        ],
        source: [
          {
            required: true, message: '请填写项目来源人名称及电话', trigger: 'blur'
          }
        ],
        owner: [
          {
            required: true, message: '请填写甲方对接人名称及电话', trigger: 'blur'
          }
        ],
        startTime: [
          {
            required: true, message: '请选择项目签订起始日期', trigger: 'change'
          }
        ],
        endTime: [
          {
            required: true, message: '请选择项目签订结束日期', trigger: 'change'
          }
        ]
      },
      itemList: [],
      showModal: false,
      // action 值分为 add edit delete
      action: ''
    }
  },
  methods: {
    formatter(row, column, value) {
      if (value === 1) {
        return '运营类项目'
      } else if (value === 2) {
        return '开发类项目'
      } else if (value === 3) {
        return '其它'
      }
    },
    formatterCommon(row, column, value) {
      return value.split('/')[0]
    },
    formatterStatus(row, column, value) {
      if (value === 1) {
        return '已签合同正在实施'
      } else if (value === 2) {
        return '已签合同未实施'
      } else if (value === 3) {
        return '未签合同正在实施'
      } else {
        return '未签合同未实施'
      }
    },
    // 项目检索
    handleQuery() {

    },
    // 重置查询表单
    handleReset(form) {
      this.$refs[form].resetFields()
      this.getItemList()
    },
    // 新增项目
    handleCreateItem() {
      this.showModal = true
      this.action = 'add'
    },
    // 关闭新增项目
    handleClose() {
      this.showModal = false
      this.handleReset('dialogForm')
    },
    // 编辑当前项目
    handleEdit(row) {
      this.showModal = true
      this.action = 'edit'
      this.$nextTick(() => {
        Object.assign(this.itemForm, row)
      })
    },
    // 删除当前项目
    handleDel(id) {
      this.$confirm('是否删除该项?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await this.$api.itemSubmit({ id, action: 'delete' })
        this.$message.success('删除成功')
        this.getItemList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 项目新增、编辑提交
    handleSubmit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          let { action, itemForm } = this
          let params = { ...itemForm, action }
          await this.$api.itemSubmit(params)
          this.showModal = false
          this.$message.success('操作成功')
          this.handleReset('dialogForm')
          this.getItemList()
        }
      })
    },
    // 获取项目列表
    async getItemList() {
      try {
        let list = await this.$api.getItemList()
        this.itemList = list
      } catch (e) {
        throw new Error(e)
      }
    }
  },
  mounted() {
    this.getItemList()
  }
}
</script>

<style lang="scss">
.query-form {
  display: flex;
  justify-content: space-between;
}

.item-manage .line {
  margin: 0 15px;
}

.item-manage .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.item-manage .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.item-manage .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 110px;
  height: 110px;
  line-height: 110px;
  text-align: center;
}

.code {
  width: 110px;
  height: 110px;
  display: block;
}

.delete-circle {
  width: 24px;
  height: 24px;
  background: rgba(0, 0, 0, 0.6);
  right: 6px;
  top: 6px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 2px auto 6px;
}

.code-close {
  color: #fff;
}

.el-rate {
  margin-top: 7px;
}
</style>